package com.sharer.last.controller;

import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.R;
import com.sharer.last.model.po.CityVisitorPO;
import com.sharer.last.model.vo.DashboardVO;
import com.sharer.last.model.vo.dashboard.CategoryForBlogCountVO;
import com.sharer.last.model.vo.dashboard.TagForBlogCountVO;
import com.sharer.last.service.BlogService;
import com.sharer.last.service.CommentService;
import com.sharer.last.service.DashboardService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 后台管理仪表盘
 *
 * @ClassName DashboardAdminController
 * @Author wangjin
 * @Date 2023/5/5 21:03
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_ADMIN_API + "/dashboard")
public class DashboardAdminController {
    @Resource
    DashboardService dashboardService;

    @Resource
    BlogService blogService;

    @Resource
    CommentService commentService;

    /**
     * 首页仪表盘数据展示
     * TODO 命名优化，map格式的数据需要优化
     *
     * @return
     */
    @GetMapping("/index")
    public R<DashboardVO> dashboard() {
        int todayPV = dashboardService.countVisitLogByToday();
        int todayUV = 0;
//        int                 todayUV              = redisService.countBySet(RedisKeyConstants.IDENTIFICATION_SET);
        long                   blogCount            = blogService.count();
        long                   commentCount         = commentService.count();
        CategoryForBlogCountVO categoryBlogCount = dashboardService.getCategoryBlogCount();
        TagForBlogCountVO      tagBlogCount      = dashboardService.getTagBlogCount();
        Map<String, List>      visitRecordMap       = dashboardService.getVisitRecordMap();
        List<CityVisitorPO>    cityVisitorList      = dashboardService.getCityVisitorList();
        DashboardVO dashboardVO = DashboardVO.builder()
                .pv(todayPV)
                .uv(todayUV)
                .blogCount(blogCount)
                .commentCount(commentCount)
                .category(categoryBlogCount)
                .tag(tagBlogCount)
                .visitRecord(visitRecordMap)
                .cityVisitor(cityVisitorList)
                .build();
        return R.ok(dashboardVO);
    }
}
